﻿using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class CustomerAppService : BaseAppService<Customer, string, CustomerSearch, ICustomerRepository>, ICustomerAppService
    {
        #region ICustomerAppService Members

        public override BoolGenericResult<Customer> AddOrUpdate(Customer entity)
        {
            Init();
            BoolGenericResult<Customer> result = new BoolGenericResult<Customer>();

            this.EmployeeAppService = this.SpringContext.GetObject(SystemModuleConst.EMPLOYEEAPPSERVICE) as IEmployeeAppService;
            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Seller seller = new Seller(employee);

            Customer oldCustomer = null;
            if (entity.Id.IsNotEmpty())
            {
                oldCustomer = this.Repository.Get(entity.Id);
            }

            if (oldCustomer.IsNull())
            {
                IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
                entity.Id = idGenerateAppService.GenerateID(SystemIdentifier.BaseCodeRuleType.Customer);
                result = seller.ContractCustomer(entity.Id, entity.AgentType, entity.Name, entity.Nature, entity.DeliveryAddress, entity.Contact, entity.Telephone, entity.Mobile, entity.Fax,
                    entity.Email, entity.Area, entity.Address, entity.AddressEnglish, entity.DeliveryAddress1, entity.DeliveryAddress2, entity.PostCode, entity.PayType, entity.DiscountRate,
                    entity.PayDay, entity.CreditLimit, entity.TaxIdentificationNumber, entity.InvoiceType, entity.Bank, entity.BankAccount, entity.Remark, entity.WareHouse);
                if (result.IsSuccess)
                    return base.Add(result.Object);
            }
            else
            {
                result = seller.UpdateCustomer(oldCustomer, entity.AgentType, entity.Name, entity.Nature, entity.DeliveryAddress, entity.Contact, entity.Telephone, entity.Mobile, entity.Fax,
                    entity.Email, entity.Area, entity.Address, entity.AddressEnglish, entity.DeliveryAddress1, entity.DeliveryAddress2, entity.PostCode, entity.PayType, entity.DiscountRate,
                    entity.PayDay, entity.CreditLimit, entity.TaxIdentificationNumber, entity.InvoiceType, entity.Bank, entity.BankAccount, entity.Remark,entity.WareHouse);
                if (result.IsSuccess)
                    return base.Update(result.Object);
            }
            return result;
        }
        #endregion
    }
}
